<template>
  <el-dialog v-model="dialogVisible" :title="skuForm.id ? '编辑sku' : '新增sku'" width="30%">
    <el-form ref="formRef" :model="skuForm" :rules="rules" label-width="100px" style="padding-right: 30px">
      <el-form-item label="图片：" prop="picture">
        <ImgUpload @sendImage="sendImage" :initialImage="skuForm.picture" />
      </el-form-item>
      <el-form-item label="sku编码：" prop="skuCode">
        <el-input v-model="skuForm.skuCode" placeholder="请输入sku编码"></el-input>
      </el-form-item>
      <el-form-item label="价格：" prop="price">
        <el-input v-model="skuForm.price" placeholder="请输入价格"></el-input>
      </el-form-item>
      <el-form-item label="库存：" prop="inventory">
        <el-input v-model="skuForm.inventory" placeholder="请输入库存"></el-input>
      </el-form-item>

      <div v-for="(spec, index) in specList" :key="index">
        <el-form-item :label="spec.name">
          <el-select v-model="skuForm.skuSpecList[index].valueName" placeholder="请选择">
            <el-option v-for="option in spec.specValueList" :key="option.id" :label="option.name"
              :value="option.name" />
          </el-select>
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { addSkuAPI, editSkuAPI } from "@/apis/goods.js";
import ImgUpload from "@/components/ImageUpload/index.vue";

// 状态和引用定义
const dialogVisible = ref(false);
const formRef = ref(null);
const skuForm = ref({
  goodsId: "",
  picture: "",
  skuCode: "",
  price: "",
  inventory: "",
  skuSpecList: [{ name: "", valueName: "" }],
});
const specList = ref([]);

// 表单验证规则
const rules = {
  picture: [{ required: true, message: "图片不能为空", trigger: "blur" }],
  skuCode: [{ required: true, message: "sku编码不能为空", trigger: "blur" }],
  price: [{ required: true, message: "价格不能为空", trigger: "blur" }],
  inventory: [{ required: true, message: "库存不能为空", trigger: "blur" }],
};

// 事件定义
const emit = defineEmits(["success"]);

const sendImage = (url) => {
  skuForm.value.picture = url;
};

const onSubmit = async () => {
  await formRef.value.validate();
  const isEdit = skuForm.value.id;
  if (isEdit) {
    await editSkuAPI(skuForm.value);
    ElMessage.success("编辑成功");
  } else {
    await addSkuAPI(skuForm.value);
    ElMessage.success("添加成功");
  }
  dialogVisible.value = false;
  emit("success");
};

const open = (row, id, specs) => {
  dialogVisible.value = true;
  skuForm.value = { ...row };
  specList.value = specs;
  skuForm.value.skuSpecList = specList.value.map((spec, index) => {
    const currentSpecValue =
      row.skuSpecList && row.skuSpecList[index]
        ? row.skuSpecList[index].valueName
        : "";
    return {
      name: spec.name,
      valueName: currentSpecValue,
    };
  });

  skuForm.value.goodsId = id;
};

defineExpose({
  open,
});
</script>